<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@include file="ajax.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <title>商品信息列表</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }

        th {
            background-color: #f2f2f2;
        }

        a{
            cursor: pointer;
        }

        .button {
            padding: 5px 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 3px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
        }

        .button-delete {
            padding: 5px 10px;
            background-color: #f44336;
            color: white;
            border: none;
            border-radius: 3px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
        }

    </style>
</head>
<body>
<script>
    function deleteGood(url){
        axiosInstance.post(
            url,
            {
                headers: headersJson
            }
        ).then((res)=>{
            console.log(res.data);
            if (res.data.code == 200){
                console.log("删除成功");
                $('#msg').innerHTML = "删除成功";
                setTimeout(()=>{
                    $('#msg').innerHTML = "";
                    loadPage('/good/getAllGoods');
                }, 1500);
            }else {
                console.log("删除失败");
                $('#msg').innerHTML = "删除失败";
                setTimeout(()=>{
                    $('#msg').innerHTML = "";
                }, 1500);
            }
        }).catch((e)=>{
            console.log(e);
            console.log("删除出现异常");
            $('#msg').innerHTML = "删除出现异常";
            setTimeout(()=>{
                $('#msg').innerHTML = "";
            }, 1500);
        })
    }
</script>
<div id="msg"></div>
<div class="flex-row-between" style="justify-content: space-between">
    <h2 id="title">商品信息列表</h2>
    <button class="button-blue" onclick="navigateTo('/good/createGood')">增加商品</button>
</div>
<table>
    <thead>
    <tr>
        <th style="text-align: center">商品名称</th>
        <th style="text-align: center">商品类型</th>
        <th style="text-align: center">销售价</th>
        <th style="text-align: center">是否推荐</th>
        <th style="text-align: center">是否特价</th>
        <th style="text-align: center">库存数</th>
        <th style="text-align: center">商品销量</th>
        <th style="text-align: center">上架日期</th>
        <th style="text-align: center">点击数</th>
        <th style="text-align: center" width="150">操作</th>
    </tr>
    </thead>
    <tbody>
    <div>${sessionScope.goodList}</div>
    <c:forEach items="${goodList}" var="good">
        <tr align="center">
            <td>${good.goodName}</td>
            <td>${good.cateName}</td>
            <td>${good.price}</td>
            <td>${good.recommend}</td>
            <td>${good.special}</td>
            <td>${good.storage}</td>
            <td>${good.sellNum}</td>
            <td>${good.createTime}</td>
            <td>${good.hits}</td>
            <td><a onclick="navigateTo('/good/getGoodsById?id=${good.goodId}')" class="button">编辑</a>&nbsp;&nbsp;<a
                    class="button-delete"
                    onclick="{if(confirm('确定要删除吗?')){deleteGood('/good/deleteGood/${good.goodId}');}return false;}">删除</a></td>
        </tr>
    </c:forEach>
    </tbody>
</table>

</body>
</html>
